<?php
/**
 * UrlsHelper.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/10/26- 1:47 下午
 */

namespace common\helpers;


use common\constants\PlatformConst;
use common\constants\PlatformConstGY;

class UrlsHelper
{


    /**
     * 通过url 分析出当前的url所属来源平台
     * @param string $url
     * @return int
     */
    public static function getUrlSource(string $url): int
    {
        $link = strtolower($url);

        if (substr($link, 0, 4) != 'http') return false;

        $source = 0;
        if (strpos($link, 'item.taobao.com')) {
            $source = PlatformConst::PLATFORM_TB;
        } elseif (strpos($link, 'detail.tmall.com')) {
            $source = PlatformConst::PLATFORM_TMALL;
        } elseif (strpos($link, 'detail.1688.com')) {
            $source = PlatformConst::PLATFORM_1688;
        } elseif (strpos($link, 'item.jd.com')) {
            $source = PlatformConst::PLATFORM_JD;
        } elseif (strpos($link, 'yangkeduo.com')) {
            $source = PlatformConst::PLATFORM_PDD;
        }

        return $source;
    }

    /**
     * 通过url 分析出当前的url所属来源平台(光云)
     * @param string $url
     * @return int
     */
    public static function getUrlSourceGY(string $url): int
    {
        $link = strtolower($url);

        if (substr($link, 0, 4) != 'http') return false;

        $source = 0;
        if (strpos($link, 'item.taobao.com')) {
            $source = PlatformConstGY::PLATFORM_TB;
        } elseif (strpos($link, 'detail.tmall.com')) {
            $source = PlatformConstGY::PLATFORM_TMALL;
        } elseif (strpos($link, 'detail.1688.com')) {
            $source = PlatformConstGY::PLATFORM_1688;
        }
        return $source;
    }


    /**
     * 通过来源和唯一值，返回商品地址
     * @param int $source
     * @param $uuid
     * @return string
     */
    public static function getUrlBySourceAndPk(int $source,$uuid)
    {
        $url = '';
        if($uuid)
        {
            switch ($source)
            {
                case PlatformConst::PLATFORM_TB:
                    $url = 'https://item.taobao.com/item.htm?id='.$uuid;
                    break;
                case PlatformConst::PLATFORM_TMALL:
                    $url = 'https://detail.tmall.com/item.htm?id='.$uuid;
                    break;
                case PlatformConst::PLATFORM_1688:
                    $url = 'https://detail.1688.com/offer/'.$uuid.'.html';
                    break;
                case PlatformConst::PLATFORM_PDD:
                    $url = 'http://mobile.yangkeduo.com/goods.html?goods_id='.$uuid;
                    break;
            }
        }
        return $url;
    }


    /**
     * 通过url获取主键信息
     * @param int $platform
     * @param string $url
     * @return int|mixed|null
     */
    public static function getUrlPrimaryKey(int $platform, string $url)
    {

        $urlData = parse_url($url);
        $urlData['query'] = $urlData['query'] ?? '';

        $primaryKey = null;
        switch ($platform) {
            case PlatformConst::PLATFORM_TB:
            case PlatformConst::PLATFORM_TMALL:
                $queryData = self::convertUrlQuery($urlData['query']);
                $primaryKey = $queryData['id'] ?? 0;
                break;
            case PlatformConst::PLATFORM_1688:
                preg_match('/offer\/(.*)\.html/', $url, $matchId);
                $primaryKey = $matchId[1] ?? 0;
                break;
            case PlatformConst::PLATFORM_JD:
                preg_match('/\.com\/(.*)\.html/', $url, $matchId);
                $primaryKey = $matchId[1] ?? 0;
                break;
            case PlatformConst::PLATFORM_PDD:
                $queryData = self::convertUrlQuery($urlData['query']);
                $primaryKey = $queryData['goods_id'] ?? 0;
                break;

        }

        return $primaryKey;
    }

    /**
     * 通过url获取主键信息(光云)
     * @param int $platform
     * @param string $url
     * @return int|mixed|null
     */
    public static function getUrlPrimaryKeyGY(int $platform, string $url)
    {

        $urlData = parse_url($url);
        $urlData['query'] = $urlData['query'] ?? '';

        $primaryKey = null;
        switch ($platform) {
            case PlatformConstGY::PLATFORM_TB:
            case PlatformConstGY::PLATFORM_TMALL:
                $queryData = self::convertUrlQuery($urlData['query']);
                $primaryKey = $queryData['id'] ?? 0;
                break;
            case PlatformConstGY::PLATFORM_1688:
                preg_match('/offer\/(.*)\.html/', $url, $matchId);
                $primaryKey = $matchId[1] ?? 0;
                break;
        }

        return $primaryKey;
    }


    public static function convertUrlQuery(string $query): array
    {
        $queryParts = explode('&', $query);

        $params = [];

        foreach ($queryParts as $param) {

            $item = explode('=', $param);

            $params[$item[0]] = $item[1];

        }

        return $params;
    }
}